#pragma once
#include<iostream>
#include<string>
#include"Connection.hpp"
#include "Log.hpp"
// #include <log.hpp>

using namespace LogModule;
#define SIZE 1024
class Channel:public Connection
{
    public:
    Channel(int sockfd,const InetAddr& client_addr):_sockfd(sockfd),_client_addr(client_addr)
    {
        //将这个套接字设置为非阻塞
        SetNonBlock(sockfd);
    }

    
    void Recver() override
    {
        char buffer[SIZE];
        while (true)
        {
            buffer[0]=0;

            //接收对应Channel中的_sockfd的套接字数据,并写入到inbuffer中
            ssize_t n=recv(_sockfd,buffer,SIZE-1,0);
            if(n>0)
            {
                buffer[n]=0;
                inbuffer+=buffer;
                LOG(LogLevel::INFO)<<"执行Recver";
            }
            //对端关闭
            else if(n==0)
            {
                //删除所属的Reactor监听对应的套接字
                Excepter();
                return;
            }
            else
            {
                if(errno==EAGAIN||errno==EWOULDBLOCK)
                {
                    break;
                }
                else if (errno==EINTR)
                {
                    continue;
                }
                else
                {
                    //删除所属的Reactor监听对应的套接字
                    Excepter();
                    return;
                }
                
            }
        }

        // LOG(LogLevel::FATAL)<<"LISTEN ERROR";
        LOG(LogLevel::DEBUG) << "CHANNEL:INBUFFER:\n" << inbuffer;

        //处理Channel的输入
        if(!inbuffer.empty())
        {
            outbuffer+=_handler(inbuffer);
        }
        if(!outbuffer.empty())
        {
            Sender();
        }
        
    }
    void Sender() override
    {
        while (true)
        {
            ssize_t n=send(_sockfd,outbuffer.c_str(),outbuffer.size(),0);
            if(n>0)
            {
                outbuffer.erase(0,n);
                if(outbuffer.empty())
                {
                    return;
                }
            }
            else if(n==0)
            {
                break;
            }
            else
            {
                if(errno==EAGAIN||errno==EWOULDBLOCK)
                break;
                if (errno==EINTR)
                continue;
                else
                {
                    Excepter();
                    return;
                }
                
            }
        }
        if (!outbuffer.empty())
        {
            GetOwner()->EnableReadWrite(_sockfd,true,true);
        }
        else
        {
            GetOwner()->EnableReadWrite(_sockfd,true,false);
        }
        
    }
    void Excepter() override
    {
        GetOwner()->DelConnection(_sockfd);
    }
    int GetSockFd() override
    {
        return _sockfd;
    }
    std::string& Inbuffer()
    {
        return inbuffer;
    }
    void AppendOutBuffer(const std::string& out)
    {
        outbuffer+=out;
    }
    ~Channel()
    {

    }
    private:
    int _sockfd;
    std::string inbuffer;
    std::string outbuffer;
    InetAddr _client_addr;
};